<?php

/**
 * 获取用户IP
 * @return [type] [description]
 */
function getIp()
{
    if (getenv("HTTP_CLIENT_IP")) {
        $ip = getenv("HTTP_CLIENT_IP");
    } else if (getenv("HTTP_X_FORWARDED_FOR")) {
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    } else if (getenv("REMOTE_ADDR")) {
        $ip = getenv("REMOTE_ADDR");
    } else {
        $ip = "";
    }
    return $ip;
}

function exceptionLog($e)
{
    //$content = 'jsonData:' . json_encode($requestData) . '-message:' . $message;
    Storage::append('exception.log', date('Y-m-d H:i:s') . '--file:' . $e->getFile() . '--line:' . $e->getLine() . '--message:' . $e->getMessage());
}

function errorLog($postData, $message)
{
    $content = 'jsonData:' . json_encode($postData) . '-message:' . $message;
    Storage::append('error.log', date('Y-m-d H:i:s') . '--' . $content);
}

function apiLog($data)
{
    Storage::append('api.log', date('Y-m-d H:i:s') . '--' . json_encode($data));
}

function cronLog($message)
{
    $message = date('Y-m-d H:i:s') . '--' . $message;
    Storage::append('cron_job.log', $message);
}

function domainTechRouteList($techRouteList)
{
    $domainList = [];
    foreach ($techRouteList as $key => $value) {
        $techDomainList = explode(',', $value['domain']);
        $techDomainList = array_filter($techDomainList);
        foreach ($techDomainList as $key1 => $value1) {
            if (@$domainList[$value1]) {
                array_push($domainList[$value1]['techRouteList'], ['id' => $value['id'], 'name' => $value['name']]);
            } else {
                $domainList[$value1] = [
                    'id' => $value1,
                    'techRouteList' => [
                        ['id' => $value['id'], 'name' => $value['name']],
                    ],
                ];
            }

        }
    }
    return $domainList;
}

/**
 * 根据数组获取相应字段组成的字符串
 * $array = [
 *         ['id'=>'','name'=>'']
 *         ['id'=>'','name'=>'']
 *         ['id'=>'','name'=>'']
 *     ]
 * $stringType 为id ,则传回ID组成的字符串
 * @param  [type] $array      [description]
 * @param  string $stringType [id]
 * @return [type]             [description]
 */
function getArrayToString($array, $stringType = 'id', $implodeType = ',')
{
    if (!$array) {
        return [];
    }
    $arr = [];
    foreach ($array as $key => $value) {
        $arr[] = $value[$stringType];
    }
    return implode($implodeType, $arr);
}
